package cn.ren.service;

import cn.ren.dao.CheckGroupDao;
import cn.ren.dao.CheckItemDao;
import cn.ren.entity.PageResult;
import cn.ren.entity.QueryPageBean;
import cn.ren.pojo.CheckGroup;
import cn.ren.pojo.CheckItem;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import okhttp3.internal.Internal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: itcast_health
 * @description:
 * @author: ren
 * @create: 2020-02-22 16:58
 **/
@Service(interfaceClass = CheckGorupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGorupService {
    @Autowired
    private CheckItemDao checkItemDao;
    @Autowired
    private CheckGroupDao checkGroupDao;

    @Override
    public List<CheckItem> findAll() {
        return checkItemDao.findAll();
    }

    @Override
    public void add(CheckGroup checkGroup, Integer[] ints) {
        checkGroupDao.add(checkGroup);
//        System.out.println("id值："+checkGroup.getId());
        setCheckGroupAndCheckItem(checkGroup.getId(),ints);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<CheckGroup> page = checkGroupDao.selectByCondition(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public List<CheckGroup> findAllGroup() {
        List<CheckGroup> list = checkGroupDao.findAll();
        return list;
    }

    @Override
    public CheckGroup findById(int id) {
        return checkGroupDao.findById(id);
    }

    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(int id) {
        return checkGroupDao.findCheckItemIdsByCheckGroupId(id);
    }

    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        //先根据id删除中间表数据，再根据checkitemIds，加入数据
        checkGroupDao.deleteAssociation(checkGroup.getId());
        //向中间表(t_checkgroup_checkitem)插入数据（建立检查组和检查项关联关系）
        setCheckGroupAndCheckIte(checkGroup.getId(),checkitemIds);
        checkGroupDao.edit(checkGroup);
//更新检查组基本信息
    }
    //向中间表(t_checkgroup_checkitem)插入数据（建立检查组和检查项关联关系）
    public void setCheckGroupAndCheckIte(Integer checkGroupId,Integer[] checkitemIds){
        if(checkitemIds != null && checkitemIds.length > 0){
            for (Integer checkitemId : checkitemIds) {
                HashMap<String,Integer> map = new HashMap<>();
                map.put("checkGroupId",checkGroupId);
                map.put("checkItemId",checkitemId);
                checkGroupDao.setCheckGroupAndCheckItem(map);
            }
        }
    }

    /**
     * 往中间表里加数据
     * @param checkGroupId
     * @param checkItemIds
     */
    public void setCheckGroupAndCheckItem(Integer checkGroupId, Integer[] checkItemIds){
        //判断数组是否为空，排除空指针异常
        if (checkItemIds != null && checkItemIds.length > 0){
            //遍历，以map配对来增加到中间表里
            for (int i = 0; i < checkItemIds.length; i++) {
                HashMap<String, Integer> map = new HashMap<>();
                map.put("checkGroupId",checkGroupId);
                map.put("checkItemId",checkItemIds[i]);
                checkGroupDao.setCheckGroupAndCheckItem(map);
            }
        }
    }
}
